package com.example.lianjuan.ui.frament;

import android.content.Intent;
import android.media.Image;
import android.os.Handler;
import android.os.Looper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager2.widget.ViewPager2;

import com.example.lianjuan.Base.BaseFragment;
import com.example.lianjuan.Model.domain.Categories;
import com.example.lianjuan.Presenter.impl.IHomePresenter;
import com.example.lianjuan.Presenter.impl.impl.HomePresenterImpl;
import com.example.lianjuan.R;
import com.example.lianjuan.Utils.LogUtils;
import com.example.lianjuan.Utils.PresenterManang;
import com.example.lianjuan.View.IHomeCallback;
import com.example.lianjuan.ui.activeity.MainActivity;
import com.example.lianjuan.ui.activeity.ScannerActivity;
import com.example.lianjuan.ui.adapter.HomePager2Adapter;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.tamsiree.rxfeature.activity.ActivityScanerCode;

import java.util.List;

public class HomeFragment extends BaseFragment implements IHomeCallback, TabLayoutMediator.TabConfigurationStrategy {
    private IHomePresenter mHomePresenter;
    private HomePager2Adapter mHomePager2Adapter;
    private Categories categories;
    private TabLayoutMediator mTabLayoutMediator;
    private ViewPager2 mHomePager2;
    private TabLayout mHomeIndicator;
    private EditText mEditSearchBox;
    private ImageView ScanBtn;

    @Override
    protected View loadRootView(LayoutInflater inflater, @Nullable ViewGroup container) {
        return inflater.inflate(R.layout.base_home_fragment, container, false);
    }

    @Override
    protected int getRootViewResID() {
        return R.layout.fragment_home;
    }

    @Override
    protected void release() {
        //取消回调注册
        if (mHomePresenter != null) {
            mHomePresenter.unregisterViewCallback(this);
        }
    }

    @Override
    protected void initPresenter() {
        //创建
        mHomePresenter = PresenterManang.getInstance().getHomePresenter();
        mHomePresenter.registerViewCallback(this);
    }

    @Override
    protected void initView(View view) {
        mEditSearchBox = view.findViewById(R.id.home_fragment_edit);
        mHomePager2 = view.findViewById(R.id.home_pager2);
        mHomeIndicator = view.findViewById(R.id.home_indicator);
        ScanBtn = view.findViewById(R.id.scan_icon);
        //创建ViewPager2适配器
        mHomePager2Adapter = new HomePager2Adapter(getChildFragmentManager(), getLifecycle());
        mHomePager2.setAdapter(mHomePager2Adapter);
        mTabLayoutMediator = new TabLayoutMediator(mHomeIndicator, mHomePager2, this);
        mTabLayoutMediator.attach();
    }

    @Override
    protected void loadData() {
        mHomePresenter.getCategory();
    }

    @Override
    protected void initListener() {
        //点击跳转到相机
        ScanBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getContext(), ScannerActivity.class));
            }
        });
        //viewPager2父处理，子不处理
        //mHomePager2.setUserInputEnabled(true);
        //点击搜索框跳转到SearchFragment界面
        mEditSearchBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentActivity activity = getActivity();
                if (activity instanceof MainActivity) {
                    ((MainActivity) activity).switch2Search();
                }
            }
        });
        //网络错误点击重新加载
        networkClick.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mHomePresenter != null) {
                    mHomePresenter.getCategory();
                }
            }
        });
    }

    @Override
    public void onCategoriseLoaded(Categories categories) {
        //加载的数据从这里回来
        this.categories = categories;
        if (categories.getData().size() != 0) {
            setUpState(State.SUCCESS);
        } else {
            setUpState(State.LOADING);
        }
        if (mHomePager2Adapter != null) {
            mHomePager2Adapter.setCategory(categories);
        }
    }

    /**
     * 网路错误界面
     */
    @Override
    public void onNetworkError() {
        setUpState(State.ERROR);
    }

    /**
     * 空白界面
     */
    @Override
    public void onLoadEmpty() {
        setUpState(State.EMPTY);
    }

    /**
     * 正常界面
     */
    @Override
    public void onLoadNone() {
        setUpState(State.SUCCESS);
    }

    /**
     * 加载界面
     */
    @Override
    public void onLoadLoading() {
        setUpState(State.LOADING);
    }

    @Override
    public void onLoadError() {

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mHomePresenter = null;
        if (mTabLayoutMediator != null) {
            mTabLayoutMediator.detach();
        }
    }

    //绑定Tab的数据
    @Override
    public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
        List<Categories.DataDTO> data = categories.getData();
        if (data.size() != 0) {
            tab.setText(data.get(position).getTitle());
        }
    }

}
